﻿/*
 * Created by SharpDevelop.
 * User: Jobe
 * Date: 13.01.2007
 * Time: 18:45
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Configuration;
using System.Windows.Forms;
using System.IO;

namespace PlugIn
{
	/// <summary>
	/// Description of AppSettings.
	/// </summary>
	public class AppSettings
	{
		
		private Configuration conf = null;
		
		public AppSettings(string AppName){
			/*
			ExeConfigurationFileMap FileMap = new ExeConfigurationFileMap();
			FileMap.ExeConfigFilename			= AppName+".ini";
			FileMap.LocalUserConfigFilename		= FileMap.ExeConfigFilename;
			FileMap.MachineConfigFilename		= FileMap.ExeConfigFilename;
			FileMap.RoamingUserConfigFilename	= FileMap.ExeConfigFilename;
			this.conf = ConfigurationManager.OpenMappedExeConfiguration(FileMap, ConfigurationUserLevel.PerUserRoaming);
			*/
			this.conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
		}
		
		public AppSettingsSection GetAppSettingsSection( string name )
		{
			if( this.conf.Sections.Get(name) == null )this.conf.Sections.Add(name,new AppSettingsSection() );
			this.conf.Sections.Get(name).SectionInformation.ForceSave = true;
			return (AppSettingsSection)this.conf.Sections.Get(name);
		}
		
		public void SaveSetting( AppSettingsSection app, string key, string val ){
			if(app.Settings[key] == null)app.Settings.Add( key, val ); else app.Settings[key].Value = val;
		}
		
		public string LoadSetting( AppSettingsSection app, string key ){
			if(app.Settings[key]!=null)return app.Settings[key].Value;else return "";
		}
		
		public void SaveLayout( AppSettingsSection app, Control ctrl ){
			
			switch ( ctrl.GetType().Name ){
				case "frmMain":
					SaveSetting( app, ctrl.Name+"WindowState", ((Form)ctrl).WindowState.ToString() );
					SaveSetting( app, ctrl.Name+"Height", ctrl.Height.ToString() );
					SaveSetting( app, ctrl.Name+"Width", ctrl.Width.ToString() );
					SaveSetting( app, ctrl.Name+"Top", ctrl.Top.ToString() );
					SaveSetting( app, ctrl.Name+"Left", ctrl.Left.ToString() );
					break;
				case "TreeListView":
				case "ListView":
					foreach( ColumnHeader ch in ((ListView)ctrl).Columns ){
						SaveSetting( app, ctrl.Name+ch.Index.ToString()+"Width", ch.Width.ToString() );
					}
					break;
				case "SplitContainer":
					SaveSetting( app, ctrl.Name+"SplitterDistance", ((SplitContainer)ctrl).SplitterDistance.ToString() );
					break;
			}
			
			foreach(  Control Element in ctrl.Controls )this.SaveLayout( app, Element );

		}
		
		public void LoadLayout( AppSettingsSection app, Control ctrl ){
			
			switch ( ctrl.GetType().Name ){
				case "frmMain":
					if(app.Settings[ctrl.Name+"Height"]	!= null)ctrl.Height	= Convert.ToInt32(app.Settings[ctrl.Name+"Height"].Value	);
					if(app.Settings[ctrl.Name+"Width"]	!= null)ctrl.Width	= Convert.ToInt32(app.Settings[ctrl.Name+"Width"].Value		);
					if(app.Settings[ctrl.Name+"Top"]	!= null)ctrl.Top	= Convert.ToInt32(app.Settings[ctrl.Name+"Top"].Value		);
					if(app.Settings[ctrl.Name+"Left"]	!= null)ctrl.Left	= Convert.ToInt32(app.Settings[ctrl.Name+"Left"].Value		);
					break;
				case "TreeListView":
				case "ListView":
					foreach( ColumnHeader ch in ((ListView)ctrl).Columns ){
						if(app.Settings[ctrl.Name+ch.Index.ToString()+"Width"] != null){
							ch.Width = Convert.ToInt32(app.Settings[ctrl.Name+ch.Index.ToString()+"Width"].Value );
						}
					}
					break;
				case "SplitContainer":
					if(app.Settings[ctrl.Name+"SplitterDistance"] != null)((SplitContainer)ctrl).SplitterDistance	= Convert.ToInt32(app.Settings[ctrl.Name+"SplitterDistance"].Value );
					break;
			}
			
			foreach(  Control Element in ctrl.Controls )this.LoadLayout( app, Element );

		}
		
		public void Save(){
			this.conf.Save(ConfigurationSaveMode.Modified);
		}
	}
}
